<template>
  <div>
    <n-bar></n-bar>
    <!-- 地址列表 -->
    <div >
      <van-address-list
        v-model="chosenAddressId"
        :list="addlist"
        default-tag-text="默认"
        @add="toadd"
        @edit="toedit"
        @select="select"
      ></van-address-list>
    </div>
  </div>
</template>

<script>
import { Addresslist } from "../request/app";
import qsString  from 'querystring'
export default {
  data() {
    return {
      chosenAddressId: "1",
      addlist: [],
    };
  },
  methods: {
    //   点击新增按钮时触发
    toadd() {
      this.$router.push("/Addressadd");
    },
    //   点击编辑按钮时触发
    toedit(e) {
        //  console.log(e);
      // 因为编辑要数据回显
      this.$router.push('/Addressadd?'+qsString.stringify(e))
    },
    //   点击切换选择地址时触发
    select(e) {
      // console.log(e);
      //  跳转订单页携带ID为了一会渲染地址
      this.$router.push("/order?id=" + e.id);
    },
  },
  mounted() {
    // 调取地址列表接口
    Addresslist({
      uid: sessionStorage.getItem("userinfo")
        ? JSON.parse(sessionStorage.getItem("userinfo")).uid
        : null,
    }).then((res) => {
      if (res.code == 200) {
        console.log(res, "响应");
        this.addlist = res.list.map((item) => {
          return {
            id: item.id,
            name: item.username,
            tel: item.userphone,
            address: item.location,
            areaCode: item.regioncode,
            addressDetail: item.useraddress,
            isDefault: item.status ? true : false,
            radio: item.label,
          };
        });
      }
    });
  },
};
</script>

<style scoped>
</style>
